﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using Core.Library;
using DataObject.Cache;

namespace DataObject.News
{
    public class NewsController
    {
        /***************************************
        = Back End
        -------------------------------------- */
        public static void News_Create(NewsObject o)
        {
            NewsDAL.News_Create(o);
        }

        public static void News_Update(NewsObject o)
        {
            NewsDAL.News_Update(o);
            CacheController.RemoveCachePort(Constants.ManagerCacheGroup.News);
        }

        public static void News_Update_Status(long id, int status, string approvedBy)
        {
            NewsDAL.News_Update_Status(id, status, approvedBy);
            CacheController.RemoveCachePort(Constants.ManagerCacheGroup.News);
        }

        public static NewsObject News_GetById(long id)
        {
            return ObjectHelper.MapRecord<NewsObject>(NewsDAL.News_GetById(id));
        }

        public static List<NewsObject> News_GetListByTypeByStatusPaging(string keywords, DateTime sd, DateTime ed, int type, int status, int pageIndex, int pageSize)
        {
            return ObjectHelper.MapRecords<NewsObject>(NewsDAL.News_GetListByTypeByStatusPaging(keywords, sd.ToString(Constants.DATETIME_SEARCH_FORMAT), ed.ToString(Constants.DATETIME_SEARCH_FORMAT), type, status, pageIndex, pageSize));
        }

        public static int News_GetStatistic(int type, int status)
        {
            return NewsDAL.News_GetStatistic(type, status);
        }

        public static int News_Count(string keywords, DateTime sd, DateTime ed, int type, int status)
        {
            return NewsDAL.News_Count(keywords, sd.ToString(Constants.DATETIME_SEARCH_FORMAT), ed.ToString(Constants.DATETIME_SEARCH_FORMAT), type, status);
        }

        /***************************************
        = Front End
        -------------------------------------- */

        internal static DataTable News_GetHighlightHomepage(int top)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.News_GetHighlightHomepage(top, StatusHelper.IsPublished, (int)NewsType.Normal, true);
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(Constants.Table_News.Url);
                dt.Columns.Add(Constants.Table_News.EncodeTitle);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    r[Constants.Table_News.EncodeTitle] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_News.News_Title]);
                    r[Constants.Table_News.Url] =
                            UrlHelpers.NewsDetails(r[Constants.Table_News.News_ID], r[Constants.Table_News.News_Title]);
                }
            }
            return dt;
        }

        internal static DataTable News_GetHighlightList(int top)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.News_GetHighlightList(top, StatusHelper.IsPublished, (int)NewsType.Normal, true);
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(Constants.Table_News.Url);
                dt.Columns.Add(Constants.Table_News.EncodeTitle);
                dt.Columns.Add(Constants.Table_News.DisplayPublishDate);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    r[Constants.Table_News.EncodeTitle] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_News.News_Title]);
                    r[Constants.Table_News.Url] =
                            UrlHelpers.NewsDetails(r[Constants.Table_News.News_ID], r[Constants.Table_News.News_Title]);
                    DateTime date;
                    DateTime.TryParse(r[Constants.Table_News.News_PublishDate].ToString(),out date);
                    r[Constants.Table_News.DisplayPublishDate] =
                            DateTimeFunctions.FormatIn_HHMM_DDMMYYYY(date);
                }
            }
            return dt;
        }

        internal static DataTable News_GetListPaging(int pageIndex, int pageSize, List<long> exceptionId)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.News_GetListPaging(StatusHelper.IsPublished, (int)NewsType.Normal, pageIndex, pageSize, Utils.ListNewsIdToString(exceptionId));
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(Constants.Table_News.Url);
                dt.Columns.Add(Constants.Table_News.EncodeTitle);
                dt.Columns.Add(Constants.Table_News.DisplayPublishDate);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    r[Constants.Table_News.EncodeTitle] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_News.News_Title]);
                    r[Constants.Table_News.Url] =
                            UrlHelpers.NewsDetails(r[Constants.Table_News.News_ID], r[Constants.Table_News.News_Title]);
                    DateTime date;
                    DateTime.TryParse(r[Constants.Table_News.News_PublishDate].ToString(), out date);
                    r[Constants.Table_News.DisplayPublishDate] =
                            DateTimeFunctions.FormatIn_HHMM_DDMMYYYY(date);
                }
            }
            return dt;
        }

        internal static int News_Count(List<long> exceptionId)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.News_Count(StatusHelper.IsPublished, (int)NewsType.Normal, Utils.ListNewsIdToString(exceptionId));
            }

            if (dt != null && dt.Rows.Count > 0)
                    return ConvertUtility.ConvertTo<int>(dt.Rows[0][0]);
            return 0;
        }

        internal static DataTable News_GetOtherNews(int pageIndex, int pageSize, List<long> exceptionId)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.News_GetOtherNews(StatusHelper.IsPublished, (int)NewsType.Normal, pageIndex, pageSize, Utils.ListNewsIdToString(exceptionId));
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(Constants.Table_News.Url);
                dt.Columns.Add(Constants.Table_News.EncodeTitle);
                dt.Columns.Add(Constants.Table_News.OtherNewsTitle);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    r[Constants.Table_News.EncodeTitle] =
                            UnicodeUtility.EnCodeTitle(r[Constants.Table_News.News_Title]);
                    r[Constants.Table_News.Url] =
                            UrlHelpers.NewsDetails(r[Constants.Table_News.News_ID], r[Constants.Table_News.News_Title]);
                    DateTime date;
                    DateTime.TryParse(r[Constants.Table_News.News_PublishDate].ToString(), out date);
                    r[Constants.Table_News.OtherNewsTitle] =
                        string.Format("{0} ( {1} )", r[Constants.Table_News.News_Title],
                                      DateTimeFunctions.FormatIn_HHMM_DDMMYYYY(date));
                }
            }
            return dt;
        }

        internal static DataTable News_GetDetails(long id)
        {
            DataTable dt = null;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.News_GetDetails(id, StatusHelper.IsPublished, (int)NewsType.Normal);
            }

            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add(Constants.Table_News.DisplayPublishOnlyDate);
                dt.Columns.Add(Constants.Table_News.DisplayPublishOnlyTime);
                dt.Columns.Add(Constants.Table_News.Url);
                dt.Columns.Add(Constants.Table_News.SortUrl);
                dt.Columns.Add(Constants.Table_News.EncodedBase64Title);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    DateTime date;
                    DateTime.TryParse(r[Constants.Table_News.News_PublishDate].ToString(), out date);
                    r[Constants.Table_News.DisplayPublishOnlyDate] =
                            DateTimeFunctions.FormatIn_DDMMYYYY(date);
                    r[Constants.Table_News.DisplayPublishOnlyTime] =
                            DateTimeFunctions.FormatIn_HHMM(date);
                    r[Constants.Table_News.Url] =
                            UrlHelpers.NewsDetails(r[Constants.Table_News.News_ID], r[Constants.Table_News.News_Title]);
                    r[Constants.Table_News.SortUrl] =
                            UrlHelpers.NewsDetailsSort(r[Constants.Table_News.News_ID]);
                    r[Constants.Table_News.EncodedBase64Title] =
                            SecurityUtility.Encrypt.EncodeTo64(HttpUtility.UrlEncode(r[Constants.Table_News.News_Title].ToString()));
                }
            }
            return dt;
        }
    }
}
